import { ObstacleAllType } from './ObstacleConfig';
const { ccclass, property } = cc._decorator;

import l from "./AudioManager"
import s from "./frameworkManager"
import c from "./EngineUtil"
import u from "./MatchGameData"
import p from "./FixedElementItemCtrl"
import d from "./LevelEditorCtrl"
import h from "./LevelEditorDataMgr"
import f from "./SquareEditorItem";
enum n {
    MatchItem = 1,
    PropItem = 2,
    ObstacleItem = 3
}

const g: number[] = [5, 7, 11, 12, 15, 20, 28];

@ccclass
export default class FixedRefreshCtrl extends cc.Component {

    @property(cc.Node)
    choseNode: cc.Node = null;

    @property(cc.ScrollView)
    scrollView: cc.ScrollView = null;

    @property(cc.Node)
    btn_sure: cc.Node = null;

    @property(cc.Node)
    btn_close: cc.Node = null;

    @property(cc.Node)
    btn_reset: cc.Node = null;

    @property(cc.Node)
    btn_add: cc.Node = null;

    @property(cc.Node)
    gameTouchNode: cc.Node = null;

    @property(cc.Prefab)
    fixedPerColPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    fixedElementPrefab: cc.Prefab = null;

    public choseItem = null;
    public startTime = null;
    public startNode = null;

    constructor() {
        super();
        var t = this;
        t.choseNode = null;
        t.scrollView = null;
        t.btn_sure = null;
        t.btn_close = null;
        t.btn_reset = null;
        t.btn_add = null;
        t.gameTouchNode = null;
        t.fixedPerColPrefab = null;
        t.fixedElementPrefab = null;
        t.choseItem = null;
        t.startTime = null;
        t.startNode = null;
        return t;
    }
    public onLoad() {
        this.addEvent();
        this.addButtonEvent();
        0 == h.initGameInfo.fixedDropData.length && this.createSquare();
    };
    public start() { };
    public addEvent() {
        this.gameTouchNode.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.gameTouchNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.gameTouchNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.gameTouchNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCanCel, this);
    };
    public addButtonEvent() {
        this.addChoseBtnListen();
        this.addBtnAddItemListen();
        this.addBtnSureListen();
        this.addBtnCloseListen();
        this.addBtnResetListen();
    };
    public addChoseBtnListen() {
        for (var e = this.choseNode.children, t = 0; t < e.length; t++) {
            var a = e[t];
            if (!a.getComponent(cc.Button)) {
                a.addComponent(cc.Button);
                // a.getComponent(cc.Button).transition = 3;
                this.addButtonListen(a, this.choseNeedPlaceItem, this);
            }
        }
    };
    public addBtnResetListen() {
        if (!this.btn_reset.getComponent(cc.Button)) {
            this.btn_reset.addComponent(cc.Button);
            //this.btn_reset.getComponent(cc.Button).transition = 3;
            this.addButtonListen(this.btn_reset, this.resetData, this);
        }
    };
    public addBtnAddItemListen() {
        if (!this.btn_add.getComponent(cc.Button)) {
            this.btn_add.addComponent(cc.Button);
            //this.btn_add.getComponent(cc.Button).transition = 3;
            this.addButtonListen(this.btn_add, this.addItem, this);
        }
    };
    public addItem() {
        var e = cc.instantiate(this.fixedPerColPrefab);
        this.scrollView.content.addChild(e);
        for (var t = 0; t < 9; t++) {
            var a = cc.instantiate(this.fixedElementPrefab);
            e.addChild(a);
        }
        for (var o = this.scrollView.content.children, i = this.scrollView.content.childrenCount, r = 0; r < o.length; r++) {
            var n = o[r];
            n.name = "colItem" + r;
            for (t = 0; t < n.children.length; t++) {
                var l = (a = n.children[t]).getChildByName("seqNode");
                l.active = 0 == t;
                l.getComponent(cc.Label).string = r + 1 + "";
                a.name = "square_" + t + "_" + (i - r - 1);
            }
        }
    };
    public addBtnSureListen() {
        if (!this.btn_sure.getComponent(cc.Button)) {
            this.btn_sure.addComponent(cc.Button);
            // this.btn_sure.getComponent(cc.Button).transition = 3;
            this.addButtonListen(this.btn_sure, this.clickSure, this);
        }
    };
    public clickSure() {
        for (var e = [], t = this.scrollView.content.children, a = this.scrollView.content.childrenCount, o = a; o >= 1; o--) {
            e[a - o] = [];
            for (var i = t[o - 1], r = 0; r < i.children.length; r++) {
                var n = i.children[r].getComponent(p).getInitStr();
                e[a - o][r] = n;
            }
        }
        h.setFixedDropData(e);
        this.node.active = false;
    };
    public addBtnCloseListen() {
        if (!this.btn_close.getComponent(cc.Button)) {
            this.btn_close.addComponent(cc.Button);
            //this.btn_close.getComponent(cc.Button).transition = 3;
            this.addButtonListen(this.btn_close, this.clickClose, this);
        }
    };
    public clickClose() {
        this.node.active = false;
        this.clickSure();
    };
    public choseNeedPlaceItem(e) {
        this.choseItem = e;
        this.showOneSelect(e);
    };
    public showOneSelect(e) {
        for (var t = this.choseNode.children, a = 0; a < t.length; a++) {
            var o = t[a];
            o.getChildByName("Editor_select").active = null != e && o.name == e.name;
        }
    };
    public createSquare() {
        this.scrollView.content.removeAllChildren();
        for (var e = 0; e < 8; e++) {
            var t = cc.instantiate(this.fixedPerColPrefab);
            t.name = "colItem" + e;
            this.scrollView.content.addChild(t);
            for (var a = 0; a < 9; a++) {
                var o = cc.instantiate(this.fixedElementPrefab), i = o.getChildByName("seqNode");
                i.active = 0 == a;
                i.getComponent(cc.Label).string = e + 1 + "";
                o.name = "square_" + a + "_" + (8 - e - 1);
                t.addChild(o);
            }
        }
    };
    public setChoseSquare(e) {
        var t = this.getTouchSquare(e);
        if (t) {
            var a = this.choseItem.getComponent(f);
            t.getComponent(p).changeSp(a);
            var o = t.getComponent(p), i = o.getInitStr();
            if (!o.isHaveAdd) {
                var r = o.pType, n = o.cType;
                this.node.parent.getComponent(d).addTagItem(r, n, i);
                o.isHaveAdd = true;
            }
        }
    };
    public onTouchStart(e) {
        if (!this.choseItem) {
            s.log("没有选择元素");
            c.showManageViewToast("没有选择元素");
            return null;
        }
        var t = this.getTouchSquare(e);
        this.startNode = t;
        this.startTime = new Date().getTime();
    };
    public onTouchMove(e) {
        if (!this.choseItem) {
            s.log("没有选择元素");
            c.showManageViewToast("没有选择元素");
            return null;
        }
        this.setChoseSquare(e);
    };
    public onTouchEnd(e) {
        if (!this.choseItem) {
            s.log("没有选择元素");
            c.showManageViewToast("没有选择元素");
            return null;
        }
        this.checkClick(e);
    };
    public onTouchCanCel() {
        s.log("触摸取消===========");
    };
    public getTouchSquare(e) {
        for (var t = this.scrollView.content.convertToNodeSpaceAR(e.getLocation()), a = this.scrollView.content.children, o = null, i = 0; i < a.length; i++) {
            var r = a[i];
            r.getBoundingBox().contains(t) && (o = r);
        }
        if (null == o) return null;
        var n = o.children, l = null, c = o.convertToNodeSpaceAR(e.getLocation());
        for (i = 0; i < n.length; i++) {
            var u = n[i];
            u.getBoundingBox().contains(c) && (l = u);
        }
        if (null == l) {
            s.log("没有哪一个元素");
            return null;
        }
        return l;
    };
    public addButtonListen(e, t, a, o?) {
        void 0 === o && (o = "click");
        if (e) {
            if (!e.getComponent(cc.Button)) return;
            e.on("click", function () {
                l.getInstance().playMusic(o);
                t && t.bind(a)(e);
            }, a);
        }
    };
    public checkClick(e) {
        var t = this.getTouchSquare(e);
        if (null != t && null != this.startNode) {
            if (t.name == this.startNode.name) {
                new Date().getTime() - this.startTime >= 300 ? this.longClickSquareItem(t) : this.setChoseSquare(e);
                this.startNode = null;
                this.startTime = null;
            }
        } else s.log("触摸结束没有获取到触摸的节点");
    };
    public longClickSquareItem(e) {
        var t = e.getComponent(p);
        this.deleteItem(t);
        t.resetSp();
    };
    public deleteItem(e) {
        // var t = e.pType, a = e.cType, o = this.node.parent.getComponent(d);
        // if (t == n.ObstacleItem) {
        //     u.getTargetObstacleArrByObstacle(a).forEach( (e) => {
        //         o.updateShowTagetInfo(t, e, false, false, o.showTargetInfo, o.tagSetNode);
        //     });
        //     -1 !== g.indexOf(a) && u.getTargetObstacleArrByObstacle(a, false).forEach( (e) => {
        //         o.updateShowTagetInfo(t, e, false, false, o.bottomrefreshTargrtInfo, o.pRefreshSitNode, false);
        //     });
        // } else t == n.MatchItem && o.updateShowTagetInfo(t, a, false, false, o.showTargetInfo, o.tagSetNode);
    };
    public resetData() {
        var e = this;
        this.choseItem = null;
        this.startTime = null;
        this.startNode = null;
        for (var t = this.scrollView.content.children, a = 0; a < t.length; a++) t[a].children.forEach(function (t) {
            var a = t.getComponent(p);
            e.deleteItem(a);
        });
        this.createSquare();
        this.showOneSelect(null);
        h.setFixedDropData([]);
    };
    public recover() {
        var e = h.initGameInfo.fixedDropData;
        this.createRecoverSquare(e);
    };
    public createRecoverSquare(e) {
        var t = e.length;
        this.scrollView.content.removeAllChildren();
        for (var a = 0; a < t; a++) {
            var o = cc.instantiate(this.fixedPerColPrefab);
            o.name = "colItem" + a;
            this.scrollView.content.addChild(o);
            for (var i = 0; i < 9; i++) {
                var r = cc.instantiate(this.fixedElementPrefab), n = r.getChildByName("seqNode");
                n.active = 0 == i;
                n.getComponent(cc.Label).string = a + 1 + "";
                r.name = "square_" + i + "_" + (t - a - 1);
                o.addChild(r);
            }
        }
        this.recoverSquareData(e);
    };
    public recoverSquareData(e) {
        for (var t = e.length - 1; t >= 0; t--) for (var a = e[t], o = this.scrollView.content.children[e.length - 1 - t], i = 0; i < a.length; i++) {
            var r = o.children[i], n = a[i];
            if ("N" != n) {
                var l = n.indexOf("-"), s = n.indexOf("_"), c = null, u = null;
                if (-1 != l && -1 != s) {
                    c = n.slice(0, l);
                    u = n.slice(l + 1, s);
                } else if (-1 != l && -1 == s) {
                    var d = n.split("-");
                    c = d[0];
                    u = d[1];
                }
                "M" == c ? c = 1 : "P" == c ? c = 2 : "O" == c && (c = 3);
                var h = this.getSquareEditorItem(c, u);
                h && r.getComponent(p).changeSp(h);
            }
        }
    };
    public getSquareEditorItem(e, t) {
        for (var a = this.choseNode.children, o = 0; o < a.length; o++) {
            var i = a[o].getComponent(f);
            if (i.squareType == e) {
                if (1 == i.squareType && i.matchType == t) return i;
                if (2 == i.squareType && i.propType == t) return i;
                if (3 == i.squareType && i.obstacleAllType == t) return i;
            }
        }
        return null;
    };
}
